const Router = require('koa-router');
const router = new Router();
const uuid = require('uuid');

import User from '../../model/index';
import Orders from '../../model/order';
console.log(uuid)

router.get('/', async (ctx: any) => {
    console.log(ctx.query)
    const { username, password } = ctx.query;
    console.log(ctx.query)

    // var user: [] = await User.findOne({where: {firstName}});
    User.create({
        username: username,
        password: password,
        uId: uuid.v1()
    })
    // console.log(user[0], '===============');
    ctx.body = 'qweqweqwe'
});

router.post('/login', async (ctx: any) => {
    console.log(ctx.request.body);
    const { username, password } = ctx.request.body;
    var user = await User.findOne({ where: { 'username': username } });
    if (!user) {
        ctx.body = {status: 0, msg: '用户不存在'}
    } else {
        if (user.dataValues.password === password) {
            console.log(user.dataValues.uId, '============')
            ctx.session.uId = user.dataValues.uId
            ctx.body = {status: 1, msg: 'ok'}
        } else {
            ctx.body = {status: 0, msg: '密码错误'}
        }
    }

});

router.get('/getIndent', async (ctx: any) => {
    console.log(ctx.session.uId,'xxxxxxxxxxxxx');
    // await Orders.create({
    //     imgUrl: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1590496321923&di=e90230704713d213ff863adc2fbade17&imgtype=0&src=http%3A%2F%2Ffile.youboy.com%2Fa%2F139%2F55%2F9%2F7%2F680277.jpg%3F350*250',
    //     uId: 'de72bf10-9f30-11ea-bcb5-2b2f3601c8db',
    //     ordersId: uuid.v1(),
    //     name: '超级蓄电池，充电5分钟，浪起8小时！！！！',
    //     // name: 'fasdfasdfasdfasdfasdfasdfasdfasdfasd',
    //     targetSite: '山东省，淄博市，XXXX路',
    //     num: 1,
    //     price: 218,
    //     logistics: JSON.stringify([{time: "2020-01-29 07:22:33", site: "到达四川省成都市转运中心"}, {time: "2020-02-01 02:30:44", site: "从达四川省成都市转运中心发往山东省"}])
    // })
    if (ctx.session.uId) {
        var orders = await Orders.findAll({attributes: ['id', 'imgUrl', 'price', 'ordersId', 'name', 'targetSite', 'num', 'createdAt'], where: {uId: ctx.session.uId}});
        console.log(orders);
        // var orders = null;
        ctx.body = {status: 1, data: orders}
        if(orders) {

        } else {
            ctx.body = {status: 0, msg: '还没有订单'}
        }
    } else {
        ctx.body = {status: 0, msg: '登陆错误'}
    }
});

router.get('/getLogistics', async (ctx: any) => {
    var ordersId = ctx.query.id;
    if (!ordersId) return ctx.body = {status: 0, msg: '参数错误'}
    console.log(ctx.session.uId)
    if (!ctx.session.uId) return ctx.body = {status: 0, msg: '登陆错误'}
    var logistics = await Orders.findOne({attributes: ['id', 'logistics'], where: {ordersId}});
    if (!logistics) return ctx.body = {status: 0, msg: '暂无物流信息'}
    ctx.body = {status: 1, data: logistics}

})

console.log(router.routes());
export const api = router.routes();
